<?php

class CategoryController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionView($id) {
        try {
            $data = Category::model()->findAllByAttributes(array('parent' => $id));
            $this->render('view', array(
                'model' => $this->loadModel($id),
                'data' => $data,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex() {
        try {
            $data = Category::model()->findAll();
            $this->render('index', array(
                'data' => $data,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreate() {
        try {
            $model = new Category();
            if (isset($_POST['Category'])) {
                $model->attributes = $_POST['Category'];
                /* if (isset($_POST['Category']['parent'])) {

                  $object_file = CUploadedFile::getInstance($model, 'cover_image');
                  if ($object_file !== null) {
                  $sub_cate['file_ext'] = $object_file->type;
                  $sub_cate['old_name'] = $object_file->name;
                  $sub_cate['url'] = Yii::app()->basePath . "/../data/cover/";
                  $ran = rand(0, 999999999);
                  $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $object_file->extensionName;
                  $sub_cate['cover_image'] = $fileName;
                  $object_file->saveAs(Yii::app()->basePath . "/../data/cover/$fileName");
                  }
                  $model->validate();
                  if (!$model->hasErrors()) {
                  $this->saveCategory($model);
                  $this->redirect(array('view', 'id' => $model->id));
                  }
                  }
                  } else { */
                $model->validate();
                if (!$model->hasErrors()) {
                    $this->saveCategory($model);
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/category/index'));
                }
            }
            $this->render('create', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id) {
        try {
            $model = $this->loadModel($id);
            if (isset($_POST['Category'])) {
                $model->attributes = $_POST['Category'];
                $model->active = $_POST['Category']['active'];
                $model->parent = $_POST['Category']['parent'];
                $model->name = $_POST['Category']['name'];
                $model->order = $_POST['Category']['order'];
                $model->show_index = $_POST['Category']['show_index'];
                $model->order_index = $_POST['Category']['order_index'];
                $model->show_first_product = isset($_POST['Category']['show_first_product']) ? 1:0;
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save()) {
                        $this->redirect(array('view', 'id' => $model->id));
                    }
                }
            }
            $this->render('update', array(
                'model' => $model,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdatesub($id) {
        try {
            $sub_cate = $this->loadModelsub($id);
            $old_file = $sub_cate->cover_image;
            if (isset($_POST['SubCategory'])) {
                $sub_cate->attributes = $_POST['SubCategory'];
                $object_file = CUploadedFile::getInstance($sub_cate, 'cover_image');
                if ($object_file !== null) {
                    $sub_cate['file_ext'] = $object_file->type;
                    $sub_cate['old_name'] = $object_file->name;
                    $sub_cate['url'] = Yii::app()->basePath . "/../data/cover/";
                    $ran = rand(0, 999999999);
                    $fileName = trim(date('Y-m-d H-i-s') . $ran) . '.' . $object_file->extensionName;
                    $sub_cate['cover_image'] = $fileName;
                    $object_file->saveAs(Yii::app()->basePath . "/../data/cover/$fileName");
                    if (file_exists(Yii::app()->basePath . "/../data/cover/" . $old_file)) {
                        unlink(Yii::app()->basePath . "/../data/cover/" . $old_file);
                    }
                }
                $sub_cate->validate();
                if (!$sub_cate->hasErrors()) {
                    $sub_cate['category_id'] = $_POST['SubCategory']['category_id'];
                    if ($sub_cate->save()) {
                        $this->redirect(array('view', 'id' => $sub_cate->id));
                    }
                }
            }
            $this->render('updatesub', array(
                'model' => $sub_cate,
                'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionDelete($id) {
        try {
            if ($model = $this->loadModel($id)) {
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/category/index'));
                }
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        try {
            $model = Category::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function saveCategory($model) {
        $category = new Category();
        $category->name = isset($_POST['Category']['name']) ? $_POST['Category']['name'] : '';
        $category->active = isset($_POST['Category']['active']) ? $_POST['Category']['active'] : 0;
        $category->order = isset($_POST['Category']['order']) ? $_POST['Category']['order'] : 1;
        $category->parent = isset($_POST['Category']['parent']) ? $_POST['Category']['parent'] : 0;
        $category->show_index = isset($_POST['Category']['show_index']) ? $_POST['Category']['show_index'] : 0;
        $category->order_index = isset($_POST['Category']['order_index']) ? $_POST['Category']['order_index'] : 0;
        $model->show_first_product = isset($_POST['Category']['show_first_product']) ? 1 :0;
        $category->save();
    }
}

?>
